package com.csxy.nyf.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.csxy.nyf.R;
import com.csxy.nyf.api.VodClient;
import com.csxy.nyf.entity.EduVideo;
import com.csxy.nyf.service.EduVideoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "课程小节管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;
    //添加课程小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }
    //删除课程小节
    //TODO 删除小节的同时需要删除视频

    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable("id") String id){
        //查询云端视频Id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节中是否有对应的视频文件
        if(!StringUtils.isEmpty(videoSourceId)){
            //有就删除
            vodClient.removeAliyunVideo(videoSourceId);
        }
        eduVideoService.removeById(id);
        return R.ok();
    }

    //修改课程小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    //根据小节id查询课程小节
    @GetMapping("/getVideoById/{videoId}")
    public R getVideoById(@PathVariable("videoId") String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("eduVideo",eduVideo);
    }
}
